package com.bdqn.membersystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.membersystem.bo.Menu;
import com.bdqn.membersystem.common.Constants;
import com.bdqn.membersystem.pojo.*;
import com.bdqn.membersystem.service.AfficheService;
import com.bdqn.membersystem.service.AuFunctionService;
import com.bdqn.membersystem.service.AuUserService;
import com.bdqn.membersystem.service.InformationService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：姚岳城
 *
 * @description:登录
 * 日期：2020/4/15 12:32
 */
@RestController
@Log4j
public class AuUserController {

    @Autowired
    private AuUserService auUserService;

    @Autowired
    private InformationService informationService;

    @Autowired
    private AfficheService afficheService;

    @Autowired
    private AuFunctionService auFunctionService;

    /**
     *登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value = "/login.html")
    @ResponseBody
    public String login(String user, HttpSession session){

        AuUser login = JSONObject.parseObject(user, AuUser.class);
        AuUser auUser = auUserService.getLogin(login.getLoginCode(), login.getPassword(),login.getIsStart());
        System.out.println(auUser);
        if (auUser.getIsStart()==2) {
            log.info("该账号未被启用");
            return "failed";
        }else if (auUser== null) {
            log.info("用户不存在");
            return "nologincode";
        }else if (!auUser.getPassword().equals(login.getPassword())){
            log.info("用户密码错误");
            return "pwderror";
        }else {
            log.info("登陆成功");
            auUser.setPassword("");
            session.setAttribute("user", auUser);
            return "success";
        }
    }


    /**
     * 主页面
     * @param modelMap
     * @param session
     * @return
     */
    @GetMapping(value = "/main.html")
    public ModelAndView main(ModelMap modelMap,HttpSession session){
        AuUser user = (AuUser) session.getAttribute("user");
        List<Affiche> afficheList = afficheService.findAfficheByLoginCode(user.getLoginCode());
        //获取公告列表
        modelMap.addAttribute("afficheList",afficheList);
        //获取资讯列表
        List<Information> informationList = informationService.getPortalInfoByLoginCode(user.getLoginCode());
        modelMap.addAttribute("infoList",informationList);
        List<Menu> menuList=null;
        if (null !=user){
            Map<String,Object> mo=new HashMap<String, Object>();
            mo.put("user",user);
            //根据当前用户获取菜单列表mList
            menuList=getFuncByUser(user.getRoleId());
            //json
            if (menuList != null) {
                String json = JSONObject.toJSONString(menuList);
                mo.put("mList",json);
                session.setAttribute("mList",json);
                mo.put("infoList",informationList);
                mo.put("afficheList",afficheList);
                session.setAttribute(Constants.SESSION_BASE_MODEL,mo);
                return new ModelAndView("main",mo);
            }

        }
        return new ModelAndView("main");
    }

    /**
     * 根据当前用户角色Id获取功能列表（对应的菜单)
     * @param roleId
     * @return
     */
    protected List<Menu> getFuncByUser(Long roleId){
        List<Menu> menuList=new ArrayList<>();
        AuAuthority authority=new AuAuthority();
        authority.setRoleId(roleId);
        List<AuFunction> mList = auFunctionService.getFunctionListByRoleId(authority.getRoleId());
        if (mList != null) {
            for(AuFunction function:mList){
                Menu menu=new Menu();
                menu.setMainMenu(function);
                function.setRoleId(roleId);
                List<AuFunction> subFunctionList = auFunctionService.selectSubFunctionList(function);
                if (subFunctionList != null) {
                    menu.setSubMenus(subFunctionList);
                }
                menuList.add(menu);
            }
        }
        return menuList;
    }

    /**
     * 注销
     * @return
     */
    @RequestMapping(value = "/logout.html")
    public ModelAndView logout(HttpSession session){
        //清楚user
        session.removeAttribute("user");
        //session内的所有属性多清除
        session.invalidate();
        return new ModelAndView("index");
    }

}